<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="800"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-row :gutter="10">
        <a-col :span="24">
          <a-form-item label="头像" field="avatar">
            <sa-upload-image v-model="formData.avatar" :limit="3" :multiple="false" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="用户昵称" field="nickname">
            <a-input v-model="formData.nickname" placeholder="请输入用户昵称" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="真实姓名" field="real_name">
            <a-input v-model="formData.real_name" placeholder="请输入真实姓名" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="性别" field="sex">
            <sa-radio v-model="formData.sex" dict="gender" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="用户电话" field="mobile">
            <a-input v-model="formData.mobile" placeholder="请输入用户电话" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="电子邮箱" field="email">
            <a-input v-model="formData.email" placeholder="请输入电子邮箱" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="注册渠道" field="channel">
            <sa-select v-model="formData.channel" dict="register_channel" placeholder="请选择注册渠道" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="状态" field="status">
            <sa-radio v-model="formData.status" dict="data_status" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="新用户" field="is_new_user">
            <sa-radio v-model="formData.is_new_user" dict="yes_or_no" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import commonApi from '@/api/common'
import api from '../../api/user/user'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')
const optionData = reactive({})

let title = computed(() => {
  return '用户' + (mode == 'add' ? '-新增' : '-编辑')
})

// 表单信息
const formData = reactive({
  id: null,
  avatar: '',
  nickname: '',
  real_name: '',
  sex: null,
  mobile: '',
  email: '',
  channel: 1,
  status: 1,
  is_new_user: 1,
  login_ip: '',
  login_time: '',
  user_money: '0.00',
  total_recharge_amount: '0.00',
  user_address: '',
  latitude: '',
  longitude: '',
})

// 验证规则
const rules = {
  channel: [{ required: true, message: '注册渠道必需填写' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
}

// 初始化页面数据
const initPage = async () => {}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
